package servlets.cardapio;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.Cardapio;
import br.Categoria;
import facade.RestauranteFacade;

/**
 * Servlet implementation class CadastrarCategoriaServlet
 */
@WebServlet("/cadastrarCardapio")
public class CadastrarCardapioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		List<Categoria> categorias = new ArrayList<Categoria>();
		categorias = RestauranteFacade.listarCategoriaPorNome(request.getParameter("categoria"));
		
		String nome = request.getParameter("nome");
		String status = request.getParameter("status");
		String preco1 =  request.getParameter("preco");
		Double preco = Double.parseDouble(preco1);
		String id = request.getParameter("id");
		
		Cardapio cardapio = new Cardapio();
		cardapio.setNome(nome);
		cardapio.setPreco(preco);
		cardapio.setStatus(status);
		
		
		cardapio.setCategoria(categorias.get(0));
		
		
		
		if(!id.equals("")){
			cardapio.setId(new Long(id));
			RestauranteFacade.atualizarCardapio(cardapio);
		}else{
			RestauranteFacade.inserirCardapio(cardapio);
		}
		
		response.sendRedirect("novoCardapio");
	}

}
